package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.example.common.ui.BaseActivity;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Order;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.DataSource;
import com.mondo.financialwallet.data.entity.FeedbackResponseEntity;
import com.mondo.financialwallet.data.params.FeedbackParams;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.LocalConfigStorage;
import com.mondo.financialwallet.presentation.utils.Toasts;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FeedbackActivity extends BaseActivity implements Validator.ValidationListener {

    public static void launch(Context context) {
        context.startActivity(new Intent(context, FeedbackActivity.class));
    }

    @Order(1)
    @Bind(R.id.content_edit_text)
    @NotEmpty(messageResId = R.string.message_please_enter_feedback_content)
    EditText mContentEditText;

    @Order(2)
    @Bind(R.id.nickname_text_view)
    @NotEmpty(messageResId = R.string.message_please_enter_nickname)
    EditText mNicknameTextView;

    @Order(3)
    @Bind(R.id.mobile_number_edit)
    @NotEmpty(messageResId = R.string.hint_please_your_mobile_number)
    EditText mMobileNumberEdit;

    @Bind(R.id.submit_button) Button mSubmitButton;

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_feedback);

        // 提交反馈
        Validator validator = new Validator(this);
        validator.setValidationListener(this);
        mSubmitButton.setOnClickListener(v -> validator.validate());
    }

    private void submitFeedback() {
        DataRepository.get().customerFeedback(new FeedbackParams(
                        mNicknameTextView.getText().toString(),
                        mContentEditText.getText().toString(),
                        mMobileNumberEdit.getText().toString(),
                        LocalConfigStorage.get().getUid()),
                result -> {
                    if (result.isSuccess()) {
                        Toasts.success(R.string.message_submit_success);
                        finish();
                    } else {
                        Toasts.failure(result.getMessage());
                    }
                }
        );
    }

    @Override
    public void onValidationSucceeded() {
        submitFeedback();
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        if (!errors.isEmpty()) {
            Toasts.warning(errors.get(0).getCollatedErrorMessage(
                    FeedbackActivity.this));
        }
    }
}
